package com.xxxx.server.service.impl;

import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.pojo.MailLog;
import com.xxxx.server.mapper.MailLogMapper;
import com.xxxx.server.service.IMailLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxxx.server.config.RabbitConfig;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-09-07
 */
@Service
public class MailLogServiceImpl extends ServiceImpl<MailLogMapper, MailLog> implements IMailLogService {

    @Resource
    private MailLogMapper mailLogMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 记录新员工发送邮件事件
     *
     * @param id
     * @return
     */
    @Override
    public int insertNewEvent(Integer id) {
        MailLog mailLog = new MailLog();
        //设置msgId
        String msgId = UUID.randomUUID().toString().replaceAll("-", "");
        mailLog.setMsgId(msgId);
        //设置员工id
        mailLog.setEid(id);
        //设置投递状态
        mailLog.setStatus(-1);
        //设置路由键
        mailLog.setRouteKey(RabbitConfig.MAIL_ROUTING_KEY_NAME);
        //设置直连模式交换机
        mailLog.setExchange(RabbitConfig.MAIL_EXCHANGE_NAME);
        //设置重试次数
        mailLog.setCount(0);
        //设置创建时间
        mailLog.setCreateTime(LocalDateTime.now());
        //设置更新时间
        mailLog.setUpdateTime(LocalDateTime.now());

        //设置用于确认的消息id
        CorrelationData correlationData = new CorrelationData(msgId);
        //获取邮箱地址
        String emailAddress = employeeMapper.selectById(id).getEmail();

        //数据入库
        int result = mailLogMapper.insert(mailLog);
        //RabbitMQ发送消息
        rabbitTemplate.convertAndSend(RabbitConfig.MAIL_EXCHANGE_NAME, RabbitConfig.MAIL_ROUTING_KEY_NAME, emailAddress, correlationData);

        return result;
    }



    /**
     * 更新消息状态
     *
     * @param msgId
     */
    @Override
    public void updateStatusByMsgId(String msgId, Integer status) {
        MailLog mailLog = new MailLog();
        mailLog.setMsgId(msgId);
        mailLog.setUpdateTime(LocalDateTime.now());
        //设置消息投递状态
        mailLog.setStatus(status);

        //更新数据
        mailLogMapper.updateStatus(mailLog);
    }

    /**
     * 根据邮箱查询数据
     *
     * @param to
     */
    @Override
    public MailLog selectInfoByEmail(String to) {
        MailLog mailLog = mailLogMapper.selectInfoByEmail(to);
        return mailLog;
    }

    /**
     * 获取全部投递失败的数据
     *
     * @return
     */
    @Override
    public List<MailLog> listAllFailed() {
        return mailLogMapper.listAllFailed();
    }

    /**
     * 消息发送重试，更新数据
     *
     * @param mailLog
     */
    @Override
    public void updateCount(MailLog mailLog) {
        mailLog.setStatus(0);
        mailLog.setUpdateTime(LocalDateTime.now());
        mailLog.setCount(mailLog.getCount() + 1);
        mailLog.setTryTime(LocalDateTime.now());

        mailLogMapper.updateStatus(mailLog);
    }

    @Override
    public String getEmailByMsgId(String msgId) {
        return mailLogMapper.getEmailByMsgId(msgId);
    }

    @Override
    public MailLog getMaillogByMsgId(String msgId) {
        return  mailLogMapper.getMaillogByMsgId(msgId);
    }


}
